﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Collections;

namespace CPTECH.Repository
{
    class CentroCustoDB: BaseDB
    {
        private static CentroCustoDB instance;

        private CentroCustoDB() { }

        public static CentroCustoDB getInstance()
        {
            if (instance == null)
            {
                instance = new CentroCustoDB();
            }
            return instance;
        }

        //retorna todos os locais na base
        public ArrayList selectAllCcusto()
        {
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select cd_fis_ccusto, ds_ccusto from TB_CCUSTO;", this.conn);
                SqlCeDataReader rdr = command.ExecuteReader();
                ArrayList result = new ArrayList();

                while (rdr.Read())
                {
                    result.Add(rdr.GetInt64(0).ToString() + " = " + rdr.GetString(1));
                }

                return result;
            }
            finally
            {
                this.close();
            }

        }

        //checa se código centro de custo já existe.
        public bool checkCodCcusto(Int64 codCcusto)
        {
            bool result = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select * from TB_CCUSTO where cd_fis_ccusto =@codCcusto;", this.conn);
                command.Parameters.AddWithValue("@codCcusto", codCcusto);
                SqlCeDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    result = true;
                }
            }
            finally
            {
                this.close();
            }
            return result;
        }

        //inseri novo Centro de Custo.
        public bool insertNewCcusto(string os, Int64 codCcusto, string descCcusto, string dtRegistro, byte enviado)
        {
            bool resposta = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("insert into TB_CCUSTO values('" + os + "','" + codCcusto + "','" + descCcusto +
                                                        "','" + dtRegistro + "','" + enviado + "');", this.conn);
                command.ExecuteNonQuery();
                resposta = true;
            }
            catch (FormatException)
            {
                resposta = false;
            }
            finally
            {
                this.close();
            }
            return resposta;
        }
    }
}
